import React, { Component } from "react";
import PropTypes from "prop-types"; //类型限制
import { nanoid } from "nanoid"; //唯一id库
import "./index.css";
export default class Header extends Component {

  // 对接收的props进行：类型必要性的限制
  static propTypes = {
    addTodo: PropTypes.func.isRequired,
  }

  headleKeyUp = (event) => {
    const { keyCode, target } = event;
    if (keyCode !== 13) return;
    if (target.value.trim() === "") {
      alert("输入的值不能为空");
    } else {
      //父组件传递过来的方法     往父组件中传递参数
      this.props.addTodo({ id: nanoid(), name: target.value, done: false });
    }
  };
  render() {
    return (
      <div className="todo-header">
        <input
          type="text"
          onKeyUp={this.headleKeyUp}
          placeholder="请输入你的任务名称，按回车键确认"
        />
      </div>
    );
  }
}
